package com.kaikai.educenter.controller;


import com.kaikai.commonutils.JwtUtils;
import com.kaikai.commonutils.R;
import com.kaikai.educenter.entity.UcenterMember;
import com.kaikai.educenter.entity.vo.LoginVo;
import com.kaikai.educenter.entity.vo.RegisterVo;
import com.kaikai.educenter.service.UcenterMemberService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.*;
import vo.CommentUserInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author yujiankai
 * @since 2021-05-29
 */
@RestController
@RequestMapping("/ucenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    UcenterMemberService ucenterMemberService;
    @PostMapping("loginUser")
    public R login(@RequestBody LoginVo loginVo){
        String token=ucenterMemberService.login(loginVo);
        return R.ok().data("token",token);
    }


    @PostMapping("registerUser")
    public R register(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return R.ok();
    }


    @GetMapping("getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
        //获取request,用token得到用户的id,
         String memberId = JwtUtils.getMemberIdByJwtToken(request);
         // 进而查到用户返回
        UcenterMember member = ucenterMemberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

    //根据用户id获得用户信息
    @GetMapping("getUserInfo/{id}")
    public CommentUserInfo getUserInfo(@PathVariable String id){
        UcenterMember ucenterMember = ucenterMemberService.getById(id);
        CommentUserInfo commentUserInfo=new CommentUserInfo();
        BeanUtils.copyProperties(ucenterMember,commentUserInfo );
        return commentUserInfo;
    }
    //统计一天注册的人数
    @GetMapping("getRegCountByDay/{day}")
    public R getRegCountByDay(@PathVariable String day){
        Integer count=ucenterMemberService.getRegisterCountByDay(day);
        return R.ok().data("countRegister",count);
    }

}

